<?php
namespace App\Service\Admin;

class TaskService extends BaseService
{
    //列表
    public static function index($pages, $pas = [])
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbYwTask)->where('del', 0);
        if (isset($pas['depart_id']) && $pas['depart_id']) {
            $db = $db->where('depart_id', $pas['depart_id']);
        }
        if ($pas['sort_id'] == 2) {
            $db = $db->orderBy('uptime', 'desc');
        } else {
            $db = $db->orderBy('uptime', 'asc');
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '员工不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $departOne = (array) self::dbConn(self::$dbStDepart)
                ->where('id', $v->depart_id)
                ->first();
            $depart_name = empty($departOne) ? '-' : $departOne['title'];
            $staffOne = (array) self::dbConn(self::$dbStaff)
                ->where('id', $v->staff_id)
                ->first();
            $staff_name = empty($staffOne) ? '-' : $staffOne['title'];
            $arys[] = [
                'id' => $v->id, 'title' => $v->title, 'detail' => $v->detail,
                'staff_id' => $v->staff_id, 'staff_name' => $staff_name,
                'depart_id' => $v->depart_id, 'depart_name' => $depart_name,
                //'in_day' => date('Y-m-d', strtotime($v->uptime)),
                'in_day' => $v->in_day,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //新增
    public static function add($pas)
    {
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbStaff)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $res = self::dbConn(self::$dbYwTask)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbYwTask)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbYwTask)->where('id', $id)->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbYwTask)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return self::toback(-2, '没有数据');
        }
        $res = self::dbConn(self::$dbYwTask)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}